package cn.exrick.xboot.modules.AccessControl.controller;

import cn.exrick.xboot.common.utils.IpInfoUtil;
import cn.exrick.xboot.common.utils.PageUtil;
import cn.exrick.xboot.common.utils.ResultUtil;
import cn.exrick.xboot.common.vo.PageVo;
import cn.exrick.xboot.common.vo.Result;
import cn.exrick.xboot.modules.AccessControl.entity.AccessControl;
import cn.exrick.xboot.modules.AccessControl.service.IAccessControlService;
import cn.exrick.xboot.modules.your.MyTest;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author portal
 */
@Slf4j
@RestController
@Api(description = "AccessControl管理接口")
@RequestMapping("/xboot/accessControl")
@Transactional
public class AccessControlController {

    @Autowired
    private IAccessControlService iAccessControlService;

    @Autowired
    private IpInfoUtil ipInfoUtil;

    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "通过id获取")
    public Result<AccessControl> get(@PathVariable String id){

        AccessControl accessControl = iAccessControlService.getById(id);
        return new ResultUtil<AccessControl>().setData(accessControl);
    }

    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    @ApiOperation(value = "获取全部数据")
    public Result<List<AccessControl>> getAll(){

        List<AccessControl> list = iAccessControlService.list();
        return new ResultUtil<List<AccessControl>>().setData(list);
    }

    @RequestMapping(value = "/getByPage", method = RequestMethod.GET)
    @ApiOperation(value = "分页获取")
    public Result<IPage<AccessControl>> getByPage(@ModelAttribute PageVo page){

        IPage<AccessControl> data = iAccessControlService.page(PageUtil.initMpPage(page));
        return new ResultUtil<IPage<AccessControl>>().setData(data);
    }

    @RequestMapping(value = "/insertOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "编辑或更新数据")
    public Result<AccessControl> saveOrUpdate(@ModelAttribute AccessControl accessControl){

        if(iAccessControlService.saveOrUpdate(accessControl)){
            return new ResultUtil<AccessControl>().setData(accessControl);
        }
        return new ResultUtil<AccessControl>().setErrorMsg("操作失败");
    }

    @RequestMapping(value = "/delByIds/{ids}", method = RequestMethod.DELETE)
    @ApiOperation(value = "批量通过id删除")
    public Result<Object> delAllByIds(@PathVariable String[] ids){

        for(String id : ids){
            iAccessControlService.removeById(id);
        }
        return new ResultUtil<Object>().setSuccessMsg("批量通过id删除数据成功");
    }

    @RequestMapping(value = "/ip/rights ", method = RequestMethod.GET)
    @ApiOperation(value = "外网IP和内网IP的判断")
    public void  iptest(HttpServletRequest request){
        String ipAddr = ipInfoUtil.getIpAddr(request);
        log.info("客户端IP地址"+ipAddr);
        //开始判断IP地址
        boolean b = MyTest.internalIp(ipAddr);
        log.info( b ? "内网地址ip": "外网地址ip");
    }




}
